package com.simba.server.commands.impl.web;

import com.google.gson.Gson;
import com.simba.common.command.ICommand;
import com.simba.common.log.LogUtil;
import com.simba.server.ServerParam;
import com.simba.server.base.CommonUtil;
import com.simba.server.base.TimeUtil;
import com.simba.server.components.MachineParams;
import com.simba.server.components.SortCommonDefines;
import com.simba.server.components.SorterParams;
import com.simba.server.components.data.ResultBase;
import com.simba.server.components.data.ResultInfo;
import com.simba.server.components.data.SlotInfo;
import com.simba.server.webservice.WebServiceManager;
import org.apache.log4j.Logger;

import java.util.*;

public class WebLockSlotsCmd implements ICommand {
    public static final String NAME = "WebLockSlotsCmd";
    private static final Logger logger = Logger.getLogger(WebLockSlotsCmd.class);

    private final SlotInfo[] slotInfos;

    public WebLockSlotsCmd(SlotInfo[] slotInfos) {
        this.slotInfos = slotInfos;
    }

    @Override
    public String getName() {
        return NAME;
    }

    @Override
    public Boolean run(Object params) {
        boolean ret = false;
        String remoteMethod = "AutoSort/ModifyChuteStatus";

        Map<String, Object> rp = new HashMap<>();
        //唯一标识，必须要等于分拣接口的GUID
        rp.put("Guid", TimeUtil.getTimeDateString("yyMMddHHmmss", new Date())+ CommonUtil.getRandom(1000000000L));
        //分拣机代码
        rp.put("MachineCode", MachineParams.getInstance().getDeviceId());
        //请求时间
        rp.put("RequestTime", TimeUtil.getTimeDateString("yyyy-MM-dd HH:mm:ss", new Date()));

        List<String> lockedSlots = new LinkedList<>();
        for (SlotInfo slotInfo : slotInfos) {
            if (slotInfo.getLockStatus() == SortCommonDefines.SLOT_SOFT_LOCK_FLAG){
                lockedSlots.add(slotInfo.getSlotId());
            }
        }
        //锁格格口号
        rp.put("ChuteCode", lockedSlots);

        Gson gson = CommonUtil.getGson();
        String content = gson.toJson(rp);
        try
        {
            if (SorterParams.getInstance().isDebugMode()){
                LogUtil.LogError(logger, "upload lock slot list error: debug mode doesn't upload data!");
                return false;
            }

            ResultInfo response = WebServiceManager.getInstance().doHttpServiceByPost(
                    ServerParam.REMOTE_WEBSERVICE_ADDRESS, remoteMethod, content);
            if (response != null && response.getCode() >= 200 && response.getCode() < 202)
            {
                ret = this.handleResponse(response);
            }
        }
        catch (Exception e)
        {
            LogUtil.LogError(WebLockSlotsCmd.logger, "upload lock slot list error: " + e);

        }
        return ret;
    }

    private boolean handleResponse(ResultInfo response)
    {
        boolean ret = false;
        try
        {
            ResultBase result = CommonUtil.getGson().fromJson(response.getData(), ResultBase.class);
            switch (result.getCode())
            {
                case 200:
                    ret = true;
                    break;
                default:
                    LogUtil.LogError(logger, "upload lock slot list failed : [ " + response.getCode()+" ], msg: "+response.getData());
                    break;
            }
        }
        catch (Exception e)
        {
            LogUtil.LogError(logger, "parse lock slot list result failed : " + e);
        }
        return ret;
    }
}
